package cn.com.blueInfo.HaiYuan.business.payment.service.impl;

import cn.com.blueInfo.HaiYuan.business.payment.entity.PaymentVoucher;
import cn.com.blueInfo.HaiYuan.business.payment.mapper.PaymentVoucherMapper;
import cn.com.blueInfo.HaiYuan.business.payment.service.PaymentVoucherService;
import cn.com.blueInfo.HaiYuan.business.purchase.entity.PurchaseVoucher;
import cn.com.blueInfo.utils.result.BootstrapTable;
import cn.com.blueInfo.utils.result.ResultInfo;
import cn.com.blueInfo.utils.util.JSONUtil;
import cn.com.blueInfo.utils.util.RequestPayload;
import cn.com.blueInfo.utils.util.UUIDUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 采购单据管理服务接口具体实现
 * @ClassName: PaymentVoucherServiceImpl
 * @author suxch
 * @date 2019年8月23日  上午10:53:16
 */
@Log4j2
@Service
public class PaymentVoucherServiceImpl implements PaymentVoucherService {


    @Autowired
    private PaymentVoucherMapper paymentVoucherMapper;

    /**
     * 分页查询采购单信息基本数据具体实现
     * @Title: queryPaymentVoucherDataByPage
     * @param request
     * @return BootstrapTable
     */
    @Override
    public BootstrapTable queryPaymentVoucherDataByPage(HttpServletRequest request) {
        BootstrapTable result = new BootstrapTable();
        JSONObject param = RequestPayload.analysis(request);
        log.info("bootstrapTable请求参数" + param);

        Map<String, Object> paramMap = JSON.parseObject(param.toJSONString());

        try {
            Integer count = paymentVoucherMapper.queryPaymentVoucherInfoCount(paramMap);
            List<PaymentVoucher> voucherList = paymentVoucherMapper.queryPaymentVoucherInfo(paramMap);
            result.setTotal(count);
            result.setRows(voucherList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setTotal(0);
            result.setRows(null);
        }
        return result;
    }

    /**
     * 新增采购单信息基本数据具体实现
     * @Title: addPaymentVoucherData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo addPaymentVoucherData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        if (JSONUtil.isToJSONObject(requestData)) {
            PaymentVoucher voucher = JSON.parseObject(requestData, PaymentVoucher.class);
            voucher.setUnid(UUIDUtil.createUUID());
			/*
			Map<String, Object> queryParam = new HashMap<String, Object>();
			queryParam.put("code", voucher.getCode());*/

            try {
                /*Integer isExist = paymentVoucherMapper.queryPaymentVoucherInfoCount(queryParam);*/
                Integer isExist = 0;
                if (isExist == 1) {
                    result.setResult(false);
                    result.setMessage("该采购单信息已存在，请修改");
                } else {
                    int add = paymentVoucherMapper.addPaymentVoucherInfo(voucher);
                    if (add == 1) {
                        result.setResult(true);
                        result.setMessage("采购单信息添加成功");
                    }
                }
            } catch (Exception e) {
                result.setResult(false);
                result.setError(e.getMessage());
                result.setMessage("采购单信息增加失败，请联系管理员");
                log.error(e.getMessage());
                e.printStackTrace();
            }
        } else {
            result.setResult(false);
            result.setMessage("参数异常，请联系管理员");
        }
        return result;
    }

    /**
     * 创建付款单数据具体实现
     * @Title: createPaymentVoucherData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo createPaymentVoucherData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");
        if (JSONUtil.isToJSONObject(requestData)) {
            JSONObject requestParam = JSON.parseObject(requestData);
            List<PurchaseVoucher> purchaseList = JSON.parseArray(requestParam.getJSONArray("selections").toJSONString(), PurchaseVoucher.class);
            for (int p_i = 0, p_len = purchaseList.size(); p_i < p_len; p_i++) {
                PurchaseVoucher purchase = purchaseList.get(p_i);
                if (purchase.getStatus() != 1) {
                    purchaseList.remove(p_i);
                }
            }
        }
        return result;
    }

    /**
     * 更新采购单信息基本数据具体实现
     * @Title: updatePaymentVoucherData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo updatePaymentVoucherData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        if (JSONUtil.isToJSONObject(requestData)) {
            PaymentVoucher voucher = JSON.parseObject(requestData, PaymentVoucher.class);

            try {
                int update = paymentVoucherMapper.updatePaymentVoucherInfoByUnid(voucher);
                if (update == 1) {
                    result.setResult(true);
                    result.setMessage("采购单信息更新成功");
                }
            } catch (Exception e) {
                result.setResult(false);
                result.setMessage("采购单信息更新失败，请联系管理员");
                result.setError(e.getMessage());
                log.error(e.getMessage());
                e.printStackTrace();
            }
        } else {
            result.setResult(false);
            result.setMessage("参数异常");
        }
        return result;
    }

    /**
     * 删除采购单数据具体实现
     * @Title: deletePaymentVoucherData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo deletePaymentVoucherData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestParam = request.getParameter("requestParam");
        JSONObject param = JSON.parseObject(requestParam);
        String unid = param.getString("unid");
        if (unid == null || "".equals(unid)) {
            result.setResult(false);
            result.setMessage("参数错误");
            return result;
        }

        try {
            int delete = paymentVoucherMapper.delPaymentVoucherInfoByUnid(unid);
            if (delete > 0) {
                result.setResult(true);
                result.setMessage("采购单信息删除成功");
            } else {
                result.setResult(false);
                result.setMessage("采购单信息删除失败");
            }
        } catch (Exception e) {
            result.setResult(false);
            result.setMessage("采购单信息删除失败，请联系管理员");
            result.setError(e.getMessage());
            log.error(e.getMessage());
        }
        return result;
    }

}
